package com.dameng.jianyouquan.mvp.model;

import android.text.TextUtils;

import com.dameng.jianyouquan.bean.SendVerificationBean;
import com.dameng.jianyouquan.api.NetWorkManager;
import com.dameng.jianyouquan.utils.EdittextUtils;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.ResponseBody;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ForgetPwdModel {


    public void sendVerificationCode(String phoneNum, OnSendVerificationCodeListener listener) {
        if (TextUtils.isEmpty(phoneNum)) {
            listener.sendFail("手机号不能为空");
        }else if (!EdittextUtils.isMobileNO(phoneNum)){
            listener.sendFail("请输入正确的手机号");
        } else {

            NetWorkManager.getService()
                    .getVerificationCode(phoneNum)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<ResponseBody>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(ResponseBody responseBody) {
                            String response="";
                            try {
                                response= responseBody.string();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            Gson gson = new Gson();
                            SendVerificationBean sendVerificationBean = gson.fromJson(response, SendVerificationBean.class);
                            if (sendVerificationBean.getCode()==200){
                                listener.sendSuccess();
                            }else {
                                listener.sendFail(sendVerificationBean.getMsg()+"");
                            }
                        }
                    });


        }
    }


    public void modifyPwd(String phoneNum, String verificationCode, String pwd, String pwdConfirm,OnModifyPwdListener listener) {
        if (TextUtils.isEmpty(phoneNum)||TextUtils.isEmpty(verificationCode)||TextUtils.isEmpty(pwd)||TextUtils.isEmpty(pwdConfirm)){
            listener.modifyFail("手机号或者验证码或者密码不能为空");
        }else if (!pwd.equals(pwdConfirm)){
            listener.modifyFail("两次密码输入不一致");
        }else if (!EdittextUtils.isMobileNO(phoneNum)){
            listener.modifyFail("请输入正确的手机号");
        }else if (!EdittextUtils.isPWD(pwd)){
            listener.modifyFail("密码格式不正确");
        } else {
            NetWorkManager.getService()
                    .getForgetPwd(phoneNum,pwd,verificationCode)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<ResponseBody>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(ResponseBody responseBody) {
                            String response="";
                            try {
                                response = responseBody.string();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });

        }
    }


    public interface OnSendVerificationCodeListener {
        void sendSuccess();

        void sendFail(String msg);
    }

    public interface OnModifyPwdListener {
        void modifySuccess();
        void modifyFail(String msg);
    }
}
